﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;

namespace AbpProjectTemplate.Web.Swagger
{
    public static class SwaggerAspNetCoreExtensions
    {
        public static void AddAppSwagger(this IServiceCollection services) 
        {
            // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
            services.AddSwaggerGen(options => SwaggerConfig.Configure(options));
        }

        public static void UseAppSwagger(this IApplicationBuilder app)
        {
            var personEmbeddedFileProvider = new EmbeddedFileProvider(
                typeof(SwaggerAspNetCoreExtensions).Assembly,
                "AbpProjectTemplate.Web.Swagger.Resources"
               );
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = personEmbeddedFileProvider,
                RequestPath = new PathString("/swagger")
            });

            app.UseSwagger();
            // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
            app.UseSwaggerUI(options =>
            {
                // URL: /swagger
                SwaggerConfig.ConfigureUI(options);
            }); 
        }
    }
}
